package com.base.app_core.service.user.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.base.app_core.model.ServerIP;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.Preferences;
import com.blankj.utilcode.util.StringUtils;
import com.tencent.bugly.crashreport.CrashReport;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiangheng.song on 2017/3/8.
 */
public class User implements Parcelable {

    private String mobile;
    private String wxIcon;
    private String wxNickname;
    private String wxUnionid;
    private Integer sex;
    private Double lat;
    private Double lng;
    private String createdAt;
    private Integer type;
    private String inviteCode;
    private String location;
    private Long range;
    private Integer inviteCount;
    private String provinceCode;
    private String cityCode;
    private String areaCode;
    private boolean firstLoginToday;
    private String aliAccount;
    private String realName;
    private String E;
    private String N;
    private String snCode;
    private Long id;
    private boolean pwdRedPacketEnable;
    private String tableKey;
    private boolean taskUnRevice;
    private List<ServerIP> ips;
    private String aliSnCode;


    public List<ServerIP> getIps() {
        return ips;
    }

    public void setIps(List<ServerIP> ips) {
        this.ips = ips;
    }

    public Long getId() {
        if (id == null) {
            return Preferences.getDefault().getLong(AppConstants.SP.ID, 0);
        }
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSnCode() {
        if (StringUtils.isEmpty(snCode)) {
            return Preferences.getDefault().getString(AppConstants.SP.SNCODE, null);
        }
        return snCode;
    }

    public void setSnCode(String snCode) {
        this.snCode = snCode;
    }

    public String getE() {
        return E;
    }

    public void setE(String e) {
        E = e;
    }

    public String getN() {
        return N;
    }

    public void setN(String n) {
        N = n;
    }

    public String getAliAccount() {
        return aliAccount;
    }

    public void setAliAccount(String aliAccount) {
        this.aliAccount = aliAccount;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public boolean isFirstLoginToday() {
        return firstLoginToday;
    }

    public void setFirstLoginToday(boolean firstLoginToday) {
        this.firstLoginToday = firstLoginToday;
    }

    public String getProvinceCode() {
        if (StringUtils.isEmpty(provinceCode)) {
            return Preferences.getDefault().getString(AppConstants.SP.PROVINCE_COODE, "35");
        }
        return provinceCode;
    }

    public void setProvinceCode(String provinceCode) {
        this.provinceCode = provinceCode;
    }

    public String getCityCode() {
        if (StringUtils.isEmpty(cityCode)) {
            return Preferences.getDefault().getString(AppConstants.SP.CITY_COODE, "05");
        }
        return cityCode;
    }

    public void setCityCode(String cityCode) {
        this.cityCode = cityCode;
    }

    public String getAreaCode() {
        if (StringUtils.isEmpty(areaCode)) {
            return Preferences.getDefault().getString(AppConstants.SP.AREA_COODE, "81");
        }
        return areaCode;
    }

    public void setAreaCode(String areaCode) {
        this.areaCode = areaCode;
    }


    public Integer getInviteCount() {
        return inviteCount;
    }

    public void setInviteCount(Integer inviteCount) {
        this.inviteCount = inviteCount;
    }

    public Long getRange() {
        if (range == null || range == 0) {
            range = Preferences.getDefault().getLong(AppConstants.SP.RANGE, 1000);
        }
        return range;
    }

    public void setRange(Long range) {
        this.range = range;
    }

    public String getMobile() {
        if (StringUtils.isEmpty(mobile)) {
            mobile = Preferences.getDefault().getString(AppConstants.SP.MOBILE, null);
        }
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getWxIcon() {
        return wxIcon;
    }

    public void setWxIcon(String wxIcon) {
        this.wxIcon = wxIcon;
    }

    public String getWxNickname() {
        return wxNickname;
    }

    public void setWxNickname(String wxNickname) {
        this.wxNickname = wxNickname;
    }

    public String getWxUnionid() {
        return wxUnionid;
    }

    public void setWxUnionid(String wxUnionid) {
        this.wxUnionid = wxUnionid;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Double getLat() {
        try {
            if (lat == null) {
                return lat;
            }

            if (lat != 0) {
                return lat;
            } else {
                return Double.parseDouble(Preferences.getDefault().getString(AppConstants.SP.LAT, "24.906469"));
            }
        } catch (Exception e) {
            CrashReport.postCatchedException(e);
            e.printStackTrace();
            return lat;
        }
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public Double getLng() {
        try {
            if (lng == null) {
                return lng;
            }

            if (lng != 0) {
                return lng;
            } else {
                return Double.parseDouble(Preferences.getDefault().getString(AppConstants.SP.LNG, "118.585952"));
            }
        } catch (Exception e) {
            CrashReport.postCatchedException(e);
            e.printStackTrace();
            return lng;
        }
    }

    public void setLng(Double lng) {
        this.lng = lng;
    }


    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getInviteCode() {
        return inviteCode;
    }

    public void setInviteCode(String inviteCode) {
        this.inviteCode = inviteCode;
    }

    public boolean getLogin() {
        return true;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getLocation() {
        return location;
    }

    public User() {
    }


    public boolean getPwdRedPacketEnable() {
        return pwdRedPacketEnable;
    }

    public void setPwdRedPacketEnable(boolean pwdRedPacketEnable) {
        this.pwdRedPacketEnable = pwdRedPacketEnable;
    }

    public String getTableKey() {
        return tableKey;
    }

    public void setTableKey(String tableKey) {
        this.tableKey = tableKey;
    }

    public boolean getTaskUnRevice() {
        return taskUnRevice;
    }

    public void setTaskUnRevice(boolean taskUnRevice) {
        this.taskUnRevice = taskUnRevice;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.mobile);
        dest.writeString(this.wxIcon);
        dest.writeString(this.wxNickname);
        dest.writeString(this.wxUnionid);
        dest.writeValue(this.sex);
        dest.writeValue(this.lat);
        dest.writeValue(this.lng);
        dest.writeString(this.createdAt);
        dest.writeValue(this.type);
        dest.writeString(this.inviteCode);
        dest.writeString(this.location);
        dest.writeValue(this.range);
        dest.writeValue(this.inviteCount);
        dest.writeString(this.provinceCode);
        dest.writeString(this.cityCode);
        dest.writeString(this.areaCode);
        dest.writeByte(this.firstLoginToday ? (byte) 1 : (byte) 0);
        dest.writeString(this.aliAccount);
        dest.writeString(this.realName);
        dest.writeString(this.E);
        dest.writeString(this.N);
        dest.writeString(this.snCode);
        dest.writeValue(this.id);
        dest.writeByte(this.pwdRedPacketEnable ? (byte) 1 : (byte) 0);
        dest.writeString(this.tableKey);
        dest.writeByte(this.taskUnRevice ? (byte) 1 : (byte) 0);
        dest.writeList(this.ips);
    }

    protected User(Parcel in) {
        this.mobile = in.readString();
        this.wxIcon = in.readString();
        this.wxNickname = in.readString();
        this.wxUnionid = in.readString();
        this.sex = (Integer) in.readValue(Integer.class.getClassLoader());
        this.lat = (Double) in.readValue(Double.class.getClassLoader());
        this.lng = (Double) in.readValue(Double.class.getClassLoader());
        this.createdAt = in.readString();
        this.type = (Integer) in.readValue(Integer.class.getClassLoader());
        this.inviteCode = in.readString();
        this.location = in.readString();
        this.range = (Long) in.readValue(Long.class.getClassLoader());
        this.inviteCount = (Integer) in.readValue(Integer.class.getClassLoader());
        this.provinceCode = in.readString();
        this.cityCode = in.readString();
        this.areaCode = in.readString();
        this.firstLoginToday = in.readByte() != 0;
        this.aliAccount = in.readString();
        this.realName = in.readString();
        this.E = in.readString();
        this.N = in.readString();
        this.snCode = in.readString();
        this.id = (Long) in.readValue(Long.class.getClassLoader());
        this.pwdRedPacketEnable = in.readByte() != 0;
        this.tableKey = in.readString();
        this.taskUnRevice = in.readByte() != 0;
        this.ips = new ArrayList<ServerIP>();
        in.readList(this.ips, ServerIP.class.getClassLoader());
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel source) {
            return new User(source);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    public String getAliSnCode() {
        if (aliSnCode == null) {
            return Preferences.getDefault().getString(AppConstants.SP.SNCODE, null);
        }
        return aliSnCode;
    }

    public void setAliSnCode(String aliSnCode) {
        this.aliSnCode = aliSnCode;
    }
}
